﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DX_DailyUtil_1._0._1.ks.util
{
    public class Util_Html : Util_Base
    {

        private string mHtml = null;

        /**
         * download htmlpage 
         */
        public void gainPage(string aLink)
        {
            WebClient _cli = new WebClient();
            MessageBox.Show(aLink);
            Uri _uri = new Uri(aLink);
            if(_uri != null)
            {
                _cli.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback);
                _cli.DownloadStringAsync(_uri);
            }
        }

        /**
         * callback for download htmlpage
         */
        private void DownloadStringCallback(Object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                // If the request was not canceled and did not throw
                // an exception, display the resource.
                if (!e.Cancelled && e.Error == null)
                {
                    this.mHtml = e.Result;
                    MessageBox.Show(this.mHtml);
                }
                else
                {
                    log_Message("调试", "Webclient", e.Error.ToString());
                    log_Console("调试", "Webclient", e.Error.ToString());
                }
            }
            catch(Exception ex)
            {
                log_Console("错误", "Webclient", ex.ToString());
            }
        }



        /**
         * gain page by httprequest and httpresponse
         */
        public void gainPage_wrq(string aLink)
        {
            WebRequest wrq = WebRequest.Create(aLink);
            wrq.BeginGetResponse(new AsyncCallback(OnResponseCallBack), wrq);
        }

        /**
         * call back 
         */
        protected void OnResponseCallBack(IAsyncResult ar)
        {
            try
            {
                WebRequest wrq = (WebRequest)ar.AsyncState;
                WebResponse wrs = wrq.EndGetResponse(ar);
                using (Stream responseStream = wrs.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream);
                    string retGet = reader.ReadToEnd();
                    MessageBox.Show(retGet);
                }
            }
            catch (Exception ex)
            {
                log_Console("错误", "httpreq", ex.ToString());
            }
        }

        /**
         *  extract content
         */
        public string extract()
        {
            int _len = 0;
            if (!string.IsNullOrEmpty(mHtml))
            {
                _len = mHtml.Length;
            }
            else
            {
            }

            return null;
        }

        /**
         * print
         */
        public string getHtmlPage()
        {
            return this.mHtml;
        }
    }
}
